package com.cn.crmsystem.service.approveService.impl;

import com.cn.crmsystem.mapper.approveMapper.MyTaskMapper;
import com.cn.crmsystem.pojo.*;
import com.cn.crmsystem.service.approveService.MyTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MyTaskImpl implements MyTaskService{
	@Autowired MyTaskMapper myTaskMapper;
	//全查
	@Override
	public ResultJson SelectMyAll(Integer userid) {
		ResultJson resultJson=new ResultJson();
		System.out.println(userid);
	   List<Task> list=new ArrayList<>();
		list=myTaskMapper.SelectMyAll(userid);
		Integer h;
		if(list!=null) {
			for(int i=0;i<list.size();i++)
			{
				h=list.get(i).getCreate_Id();
				User user=new User();
				user=myTaskMapper.selectLeader(h);
				if(user==null){
					break;
				}else {
					list.get(i).setUser(user);
				}
				if(i==list.size()-1){
					Map<String,Object> map3=new HashMap<>();
					map3.put("data",list);
					resultJson.getSuccess("成功",map3);
				}
			}
		}else{
			resultJson.getSuccess("失败");
		}
		return resultJson;
	}
//条件查询
	@Override
	public ResultJson SelectMyConditionAll(Task task, Login login) {
		ResultJson resultJson=new ResultJson();
		HashMap<Object,Object> map = new HashMap<>();
		map.put("login",login);
		map.put("task",task);
		List<Task> list=new ArrayList<>();
		Integer role=task.getRole();
		Integer g;
		if(role==0) {
			list=myTaskMapper.SelectMyJoinCondition(map);
			if(list!=null) {
				for(int i=0;i<list.size();i++)
				{
				g=list.get(i).getCreate_Id();
					User user=new User();
					user=myTaskMapper.selectLeader(g);
					if(user==null){
                       break;
					}else {
						list.get(i).setUser(user);
					}
					if(i==list.size()-1){
						Map<String,Object> map2=new HashMap<>();
						map2.put("任务条件查询信息",list);
						resultJson.getSuccess("成功",map2);
					}
				}
			}else{
				resultJson.getSuccess("失败");
			}
		}else{
			list=myTaskMapper.SelectMyChargeCondition(map);
			if(list!=null) {
			    Map<String,Object> map1=new HashMap<>();
			    map1.put("任务信息",list);
			    map1.put("领导人",login);
				resultJson.getSuccess("成功",map1);
			}else{
				resultJson.getSuccess("失败");
			}
		}
		return resultJson;
	}
	//查询参与者
	@Override
	public ResultJson SelectJoinner(Login login) {
		ResultJson resultJson=new ResultJson();
		List<User> list=new ArrayList<>();
		list=myTaskMapper.SelectUser(login.getUser_id());
		if(list!=null) {
			HashMap<Object,Object> map4 = new HashMap<>();
			map4.put("参与者",list);
			resultJson.getSuccess("成功",map4);
		}else{
			resultJson.getSuccess("失败");
		}
		return resultJson;
	}
//修改
	@Override
	public ResultJson UpdateMyTask(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer taskid=task.getTask_Id();
		Integer createid=task.getCreate_Id();
		java.util.Date enddate=task.getEnd_Date();
		String taskname=task.getTaskname();
		String describe=task.getDescribe();
		Integer s=myTaskMapper.UpdateMyTask(task);
		if(s>0){
			resultJson.getSuccess("成功");
		}else{
			resultJson.getFail("失败");
		}
		return resultJson;
	}
	//参与人增加
	@Override
	public ResultJson JoinnerInsert(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer taskid=task.getTask_Id();
		List<User> list=new ArrayList<>();
		list=task.getList();
		Integer userid;
		for(int i=0;i<list.size();i++) {
			userid=list.get(i).getId();
			int l=myTaskMapper.JoinnerInsert(userid,taskid);
			if(i==list.size()-1&&l>0){
				resultJson.getSuccess("成功");
			}
		}
		return resultJson;
	}
	//参与人删除
	@Override
	public ResultJson JoinnerDelete(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer taskid=task.getTask_Id();
		List<User> list=new ArrayList<>();
		list=task.getList();
		Integer userid;
		for(int i=0;i<list.size();i++) {
			userid=list.get(i).getId();
			int h=myTaskMapper.JoinnerDelete(userid,taskid);
			if(i==list.size()-1&&h>0){
				resultJson.getSuccess("成功");
			}
		}
		return resultJson;
	}
	//任务删除
	@Override
	public ResultJson DeleteMyTask(Integer taskid) {
		ResultJson resultJson=new ResultJson();
		Integer s1=myTaskMapper.DeleteMyTask(taskid);
		if(s1>0){
			resultJson.getSuccess("成功");
		}else{
			resultJson.getFail("失败");
		}
		return resultJson;
	}
	//任务增加
	@Override
	public ResultJson InsertMyTask(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer s2=myTaskMapper.InsertMyTask(task);
		if(s2>0){
			resultJson.getSuccess("成功");
		}else{
			resultJson.getFail("失败");
		}
		return resultJson;
	}
}
